package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.StatFs;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ConvertUtils;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileCompressActivity;
import flc.ast.activity.FolderActivity;
import flc.ast.activity.PdfConvertPicActivity;
import flc.ast.activity.PicConvertPdfActivity;
import flc.ast.activity.PrivacyFileActivity;
import flc.ast.databinding.FragmentFileManageBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.device.StorageUtil;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FileManageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FileManageFragment extends BaseNoModelFragment<FragmentFileManageBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_file_manage;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivPdfConvertPic.setOnClickListener(this);
        mDataBinding.ivPicConvertPdf.setOnClickListener(this);
        mDataBinding.ivFileCompress.setOnClickListener(this);
        mDataBinding.ivFilePrivacy.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPdfConvertPic://pdf转图片
                getManagePermission();
                break;
            case R.id.ivPicConvertPdf://图片转pdf
                getPicturePermission();
                break;
            case R.id.ivFileCompress://文件压缩
                startActivity(FileCompressActivity.class);
                break;
            case R.id.ivFilePrivacy://文件加密
                startActivity(PrivacyFileActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        StatFs statFs = StorageUtil.getInternalMemory();//内部存储
        //已使用
        long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
        //总内存
        long allLong = statFs.getTotalBytes();
        //已用百分比
        int progress = Math.round((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());
        mDataBinding.tvPhoneMemorise.setText(getString(R.string.all_text) + ConvertUtils.byte2FitMemorySize(allLong, 0));
        mDataBinding.pvPhoneProgress.setProgress(progress);
        mDataBinding.tvPhoneProgress.setText(progress + getString(R.string.unit_percent));
        getSdData();
    }

    private void getSdData() {
        List<StatFs> sdStatFs = StorageUtil.getSDCardMemory(mContext);
        if (CollectionUtils.isNotEmpty(sdStatFs)) {
            StatFs statFs = sdStatFs.get(0);
            //已使用
            long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
            //总内存
            long allLong = statFs.getTotalBytes();

            int progress = Math.round((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());

            mDataBinding.tvSdMemorise.setText(getString(R.string.all_text) + ConvertUtils.byte2FitMemorySize(allLong, 0));
            mDataBinding.pvSdProgress.setProgress(progress);
            mDataBinding.tvSdProgress.setText(progress + getString(R.string.unit_percent));
        } else {
            mDataBinding.tvSdProgress.setText(getString(R.string.sd_card_not_detected));
            mDataBinding.tvSdMemorise.setVisibility(View.GONE);
            mDataBinding.pvSdProgress.setVisibility(View.GONE);
            mDataBinding.tvSdUsed.setVisibility(View.GONE);
        }
    }

    private void getManagePermission() {
        StkPermissionHelper.reqManageExternalStoragePermission().reqPermissionDesc(getString(R.string.get_manage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                startActivity(PdfConvertPicActivity.class);
            }
        }).request();
    }

    private void getPicturePermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(PicConvertPdfActivity.class);
                    }
                }).request();
    }

}